package com.xc.ezplan.auth.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.xc.ezplan.auth.service.impl.AuthUserServiceImpl;
import com.xc.ezplan.core.base.EzResult;
import com.xc.ezplan.core.dto.AdminUserDTO;
import com.xc.ezplan.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lixingchen
 * @date 2022/11/15
 * @description 用户登录
 */
@RestController
@Api(tags = "用户登录")
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final AuthUserServiceImpl authUserService;

	@Log("用户登录")
	@ApiOperation("用户登录")
	@PostMapping("/login")
	public EzResult<Object> login(@RequestBody AdminUserDTO adminUserDTO) {
		String username = adminUserDTO.getUsername();
		String password = adminUserDTO.getPassword();
		SaTokenInfo saTokenInfo = authUserService.login(username, password);
		if (saTokenInfo == null) {
			return EzResult.failed("账户名或密码错误");
		}
		Map<String, String> tokenMap = new HashMap<>(2);
		tokenMap.put("token", saTokenInfo.getTokenValue());
		tokenMap.put("tokenHead", saTokenInfo.getTokenName());
		return EzResult.ok(tokenMap);
	}
}
